package com.sevennotes.classtable.ui.pages.components

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.AlertDialog
import androidx.compose.material.Button
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.material.TextField
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import com.sevennotes.classtable.R

@Composable
fun ConfirmAndCancelButtons(
  onConfirmClick: () -> Unit,
  onCancelClick: () -> Unit
) {
  Row(
    modifier = Modifier.fillMaxWidth(),
    horizontalArrangement = Arrangement.End
  ) {
    TextButton(onClick = { onCancelClick() }) {
      Text(text = "取消")
    }
    TextButton(onClick = { onConfirmClick() }) {
      Text(text = "确定", color = Color.Red)
    }
  }
}

@Composable
fun SimpleMsgDialog(
  text: String,
  onConfirm: () -> Unit
) {
  AlertDialog(
    title = { Text(text) },
    onDismissRequest = onConfirm,
    buttons = {
      Row(
        modifier = Modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.Center,
        verticalAlignment = Alignment.CenterVertically
      ) {
        TextButton(onClick = { onConfirm() }) {
          Text(text = "确定", color = Color.Red)
        }
      }
    }
  )
}

@Composable
fun SimpleComfirmDialog(
  text: String,
  onConfirmClick: () -> Unit,
  onCancelClick: () -> Unit
) {
  AlertDialog(
    title = { Text(text) },
    onDismissRequest = onCancelClick,
    buttons = {
      ConfirmAndCancelButtons(
        onConfirmClick = onConfirmClick,
        onCancelClick = onCancelClick
      )
    }
  )
}

@Composable
fun TimeSelectorDialog(
  list: List<List<String>>,
  picked: List<Int>,
  onCancelClick: () -> Unit,
  onConfirmClick: (List<Int>) -> Unit,
) {
  val insidePicked = remember { mutableStateOf(MutableList(picked.size) { picked[it] }) }
  AlertDialog(
    modifier = Modifier.fillMaxWidth(),
    title = {
      TimePicker(
        list = list,
        picked = { picked[it] },
        onPick = { l, n -> insidePicked.value[l] = n })
    },
    onDismissRequest = onCancelClick,
    buttons = {
      ConfirmAndCancelButtons(
        onConfirmClick = { onConfirmClick(insidePicked.value.toList()) },
        onCancelClick = onCancelClick
      )
    }
  )
}

//note and number change
@Composable
fun InfoChangeDialog(
  title: String = "变更事由",
  unitText: String = "分",
  note: String,
  number: Int,
  onConfirm: (String, Int) -> Unit,
  onCancel: () -> Unit,
) {
  var tnote by remember { mutableStateOf(note) }
  var tnumber by remember { mutableStateOf(number) }
  AlertDialog(
    onDismissRequest = onCancel,
    title = { Spacer(modifier = Modifier.height(15.dp)) },
    text = {
      Column(
        horizontalAlignment = Alignment.CenterHorizontally
      ) {
        TextField(
          value = tnote,
          onValueChange = { tnote = it },
          singleLine = true,
          label = { Text(text = title) }
        )
        Spacer(modifier = Modifier.height(5.dp))
        NumChanger(
          pointValue = tnumber,
          onPointChanged = { tnumber = it },
          unitText = unitText
        )
      }

    },
    buttons = {
      ConfirmAndCancelButtons(
        onConfirmClick = { onConfirm(tnote, tnumber) },
        onCancelClick = onCancel
      )
    }
  )
}

@Composable
fun BottomShareDialog(
  onCancel: () -> Unit,
  onWechatClick: () -> Unit,
  onLocalClick: () -> Unit
) {
  BottomDialog(
    onCancel = onCancel
  ) {
    Column(
      modifier = Modifier.padding(horizontal = 16.dp),
      verticalArrangement = Arrangement.Bottom
    ) {
      Row(
        modifier = Modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.Start
      ) {
        ShareIconButton(onClick = onWechatClick, title = "微信") {
          Icon(
            modifier = Modifier.size(40.dp),
            painter = painterResource(id = R.drawable.ic_wechat), contentDescription = "wechat")
        }
        ShareIconButton(onClick = onLocalClick, title = "保存本地") {
          Icon(
            modifier = Modifier.size(40.dp),
            painter = painterResource(id = R.drawable.ic_save_download), contentDescription = "save")
        }
      }
      TextButton(
        onClick = onCancel,
        modifier = Modifier.fillMaxWidth()
      ) {
        Text("取消")
      }
    }
  }
}

@Preview
@Composable
fun BottomShareDialogPreview() {
  BottomShareDialog(onCancel = { /*TODO*/ }, onWechatClick = { /*TODO*/ }) {

  }
}

@Composable
fun ShareIconButton(
  onClick: () -> Unit,
  title: String,
  icon:  @Composable () -> Unit,
) {
  IconButton(
    modifier = Modifier.padding(8.dp),
    onClick = onClick
  ) {
    Column(
      verticalArrangement = Arrangement.Center,
      horizontalAlignment = Alignment.CenterHorizontally
    ) {
      icon()
      Text(text = title)
    }
  }
}

@Composable
fun BottomDialog(
  onCancel: () -> Unit,
  content: @Composable () -> Unit
) {
  Dialog(
    onDismissRequest = onCancel,
    properties = DialogProperties(usePlatformDefaultWidth = false)
  ) {
    Box(modifier = Modifier
      .fillMaxSize()
      .clickable { onCancel() }) {
      Box(
        modifier = Modifier
          .fillMaxWidth()
          .clickable { }
          .align(Alignment.BottomCenter)
          .background(MaterialTheme.colors.background)
      ) {
        content()
      }
    }
  }
}
